<?php

namespace App\Services\Banner;

use App\Helpers\FunTool;
use App\Models\BannerModel;
use App\Models\PlazaModel;
use App\Services\AbstractBaseService;
use App\Services\Plaza\PlazaService;
use Illuminate\Support\Facades\Cache;

class BannerService extends AbstractBaseService
{
    /**
     * 返回全部图片
     */
    public static function list(int $genre): array
    {
        $key = 'banners-list_' . $genre;
        return Cache::remember($key, 60 * 60 * 10, function () use ($genre) {
            return static::listNotCache($genre);
        });
    }

    /**
     * 获取全部数据不带缓存
     * @param int $genre
     * @return array
     */
    public static function listNotCache(int $genre): array
    {
        $res  = BannerModel::query()->select(['name', 'pic', 'cur_type', 'parameters'])
            ->where('cur_status', 1)
            ->where('genre', $genre)
            ->orderBy('sort')
            ->get()->toArray();
        foreach ($res as &$v) {
            $topImage = '';
            if ($v['cur_type'] == BannerModel::TYPE_SPECIAL_SECTION) {
                $topImage = PlazaModel::query()->where('id', $v['parameters'])->value('area_images')??'';
            }
            $v['top_image'] = $topImage;
        }
        return $res;
    }

    /**
     * 更新缓存
     */
    public static function cacheWarehouse(): void
    {
        $list1 = static::listNotCache(BannerModel::GENRE_INDEX);
        $list2 = static::listNotCache(BannerModel::GENRE_PLAZA);
        Cache::put('banners-list_' . BannerModel::GENRE_INDEX, $list1, 60 * 60 * 10);
        Cache::put('banners-list_' . BannerModel::GENRE_PLAZA, $list2, 60 * 60 * 10);
    }

}
